﻿using System;
using System.Windows;
using System.Windows.Controls;
using CommomFunctions;

namespace Algoritmos_de_Ordenação
{
    /// <summary>
    /// Interaction logic for AlgDeOrdenacao.xaml
    /// </summary>
    public partial class AlgDeOrdenacao : Window
    {


        Controls controls = new Controls();
        

        Button[] btnsToUpdate;
        MenuItem[] MenuItems;
        public AlgDeOrdenacao()
        {
            InitializeComponent();
            myGrid.Children.Remove(cvMain);
            myGrid.Children.Remove(cvCompareAll);
            controls.addPermanentControls(cvMain);
            controls.addPermanentControls(cvCompareAll);
            btnsToUpdate = new Button[] { btnIncreasing, btnDecreasing, btnRandom, btnReset,
                                          caIncreasing, caDecreasing, caRandom, caReset};
            MenuItems = new MenuItem[] { smFast, smNormal, smSlow, smVeryFast, smVerySlow,
                                         caFast, caNormal, caSlow, caVeryFast, caVerySlow};

        }
                        
        private void tabControlMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            controls.stopThreads();
            TabItem selectedTab = new TabItem();
            foreach (TabItem ti in tabControlMain.Items)
            {
                controls.removeComponentes(ti);
                if (ti.IsSelected)
                    selectedTab = ti;
            }

            controls.removeDrawingRegions(cvMain); 
            controls.removeDrawingRegions(cvCompareAll);
            controls.removeTextBoxes(cvCompareAll);
                
            if (!selectedTab.Equals(tabComparaTodos))
            {
                controls.addComponents((TabType)tabControlMain.SelectedIndex, selectedTab, cvMain);
            }
            else
            {
                controls.addComponents(selectedTab, cvCompareAll);
            }
            GC.Collect();
        }
        #region Buttons Click Events

        private void btnIncreasing_Click(object sender, RoutedEventArgs e)
        {
            controls.buttonClicked(ButtonClicked.Increasing);
        }

        private void btnDecreasing_Click(object sender, RoutedEventArgs e)
        {
            controls.buttonClicked(ButtonClicked.Decreasing);
        }

        private void btnRandom_Click(object sender, RoutedEventArgs e)
        {
            controls.buttonClicked(ButtonClicked.Random);
        }

        private void btnSort_Click(object sender, RoutedEventArgs e)
        {
            controls.buttonClicked(ButtonClicked.Sort, btnsToUpdate);
        }

        private void btnPause_Click(object sender, RoutedEventArgs e)
        {
            controls.buttonClicked(ButtonClicked.Pause);
        }

        private void btnResume_Click(object sender, RoutedEventArgs e)
        {
            controls.buttonClicked(ButtonClicked.Resume);
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            controls.buttonClicked(ButtonClicked.Reset);
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            controls.buttonClicked(ButtonClicked.Stop, btnsToUpdate);
        }
        
        #endregion

        #region ComboBoxes Click Events

        private void cbCSharp_Click(object sender, RoutedEventArgs e)
        {
            if (cbCSharp.IsChecked == true)
            {
                gbCSharp.Visibility = Visibility.Visible;
            }
            else gbCSharp.Visibility = Visibility.Hidden;
        }

        private void cbCPlusPlus_Click(object sender, RoutedEventArgs e)
        {
            if (cbCPlusPlus.IsChecked == true)
            {
                gbCPlusPlus.Visibility = Visibility.Visible;
            }
            else gbCPlusPlus.Visibility = Visibility.Hidden;
        }

        private void cbTrace_Click(object sender, RoutedEventArgs e)
        {
            if (cbTrace.IsChecked == true)
            {
                controls.setShowTrace(true);
            }
            else controls.setShowTrace(false);
        }
        
        #endregion

        #region OptionsMenu Click Events
		
        private void smVerySlow_Click(object sender, RoutedEventArgs e)
        {
            controls.menuClicked(SpeedClicked.VerySlow, MenuItems);
        }

        private void smSlow_Click(object sender, RoutedEventArgs e)
        {
            controls.menuClicked(SpeedClicked.Slow, MenuItems);
        }

        private void smNormal_Click(object sender, RoutedEventArgs e)
        {
            controls.menuClicked(SpeedClicked.Normal, MenuItems);
        }

        private void smFast_Click(object sender, RoutedEventArgs e)
        {
            controls.menuClicked(SpeedClicked.Fast, MenuItems);
        }

        private void smVeryFast_Click(object sender, RoutedEventArgs e)
        {
            controls.menuClicked(SpeedClicked.VeryFast, MenuItems);
        }
                 
	    #endregion

        #region HelpMenu Click Events

        private void help_Click(object sender, RoutedEventArgs e)
        {
            Ajuda.openHelp();
        }

        private void tutorial_Click(object sender, RoutedEventArgs e)
        {
            Ajuda.openTutorial("Algoritmos de Ordenação.wmv");
        }

        private void about_Click(object sender, RoutedEventArgs e)
        {
            Ajuda.openAbout();
        }
        
        #endregion


        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            controls.stopThreads();
        }

    }
}
